﻿using UnityEngine;

public class GpuAnimation : MonoBehaviour {

    Transform[] bones;
    Vector4[] bonePaletteNew;

    MeshRenderer meshRenderer;
    Matrix4x4[] binePoses;
    public Material material;

    // Use this for initialization
    void Start ()
    {
        var skinnedMeshRenderer = gameObject.GetComponentInChildren<SkinnedMeshRenderer>();

        bones = skinnedMeshRenderer.bones;
        bonePaletteNew = new Vector4[bones.Length * 3];

        binePoses = skinnedMeshRenderer.sharedMesh.bindposes;
        skinnedMeshRenderer.enabled = false;
    }
	
	// Update is called once per frame
	void Update ()
    {
        for (int i = 0; i < bones.Length; i++)
        {
            Matrix4x4 bonePalette = transform.worldToLocalMatrix * bones[i].localToWorldMatrix * binePoses[i];
            bonePaletteNew[i * 3] = bonePalette.GetRow(0);
            bonePaletteNew[i * 3 + 1] = bonePalette.GetRow(1);
            bonePaletteNew[i * 3 + 2] = bonePalette.GetRow(2);
        }

        material.SetVectorArray("BonePalette", bonePaletteNew);
    }
}
